package ${packagePrefix}.application.service.impl;

import ${packagePrefix}.application.assembler.${domainObjectName}Assembler;
import ${packagePrefix}.client.api.${domainObjectName}ServiceI;
import ${packagePrefix}.client.dto.command.${domainObjectName}AddCmd;
import ${packagePrefix}.client.dto.command.${domainObjectName}ModifyCmd;
import ${packagePrefix}.client.dto.data.${domainObjectName}DTO;
import ${packagePrefix}.client.dto.query.${domainObjectName}GetQry;
import ${packagePrefix}.client.dto.query.${domainObjectName}ListQry;
import ${packagePrefix}.domain.gateway.${domainObjectName}Gateway;
import ${packagePrefix}.domain.model.${tableName?lower_case}.${domainObjectName};
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

<#if generateSwagger>
import io.swagger.v3.oas.annotations.tags.Tag;

</#if>
/**
 * ${entityComment}服务实现
 */
<#if generateSwagger>
@Tag(name = "${domainObjectName}Service", description = "${entityComment}服务")
</#if>
@Service
public class ${domainObjectName}ServiceImpl implements ${domainObjectName}ServiceI {

    @Autowired
    private ${domainObjectName}Gateway ${domainObjectName?uncap_first}Gateway;
    
    @Autowired
    private ${domainObjectName}Assembler ${domainObjectName?uncap_first}Assembler;
    
    @Override
    @Transactional
    public Long add(${domainObjectName}AddCmd cmd) {
        ${domainObjectName} ${domainObjectName?uncap_first} = ${domainObjectName?uncap_first}Assembler.toEntity(cmd);
        ${domainObjectName?uncap_first}.checkValid();
        return ${domainObjectName?uncap_first}Gateway.save(${domainObjectName?uncap_first});
    }

    @Override
    @Transactional
    public void modify(${domainObjectName}ModifyCmd cmd) {
        ${domainObjectName} ${domainObjectName?uncap_first} = ${domainObjectName?uncap_first}Gateway.findById(cmd.getId())
                .orElseThrow(() -> new RuntimeException("${entityComment}不存在"));
        
        ${domainObjectName?uncap_first}Assembler.updateEntity(${domainObjectName?uncap_first}, cmd);
        ${domainObjectName?uncap_first}.checkValid();
        ${domainObjectName?uncap_first}Gateway.save(${domainObjectName?uncap_first});
    }

    @Override
    public ${domainObjectName}DTO getById(${domainObjectName}GetQry qry) {
        Optional<${domainObjectName}> ${domainObjectName?uncap_first}Optional = ${domainObjectName?uncap_first}Gateway.findById(qry.getId());
        return ${domainObjectName?uncap_first}Optional.map(${domainObjectName?uncap_first}Assembler::toDTO).orElse(null);
    }

    @Override
    public List<${domainObjectName}DTO> list(${domainObjectName}ListQry qry) {
        List<${domainObjectName}> ${domainObjectName?uncap_first}List = ${domainObjectName?uncap_first}Gateway.findByCondition(qry);
        return ${domainObjectName?uncap_first}List.stream()
                .map(${domainObjectName?uncap_first}Assembler::toDTO)
                .collect(Collectors.toList());
    }
} 